package Gui;
import client.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.text.StyledEditorKit.BoldAction;



import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginUser extends JFrame
{
	ChatClient client;
	String name=null,password=null;
	public static LoginUser test;
	public JPanel panel,panel1;
 	private JTextField user_name;
	private JTextField user_password;
	public JFrame main_frame;
	ServerMsgLogIn  sermsg;
	Student student_obj;
	Teacher teacher_obj;
	SecretaryANDPrincipal secretary_Principal_obj;
	
	
	public LoginUser()
	{
		main_frame=new JFrame("main frame");
		main_frame.setSize(400, 400);
		main_frame.setVisible(true);
		this.setBounds(100, 100, 400, 400);
		getContentPane().setLayout(null);
		panel1= new JPanel();
		panel1.setBounds(0, 0, 434, 262);
		 panel = new JPanel();
		panel.setBounds(0, 0, 434, 262);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		user_name = new JTextField("123");
		user_name.setBounds(155, 65, 96, 27);
		panel.add(user_name);
		user_name.setColumns(10);
		
		JLabel lblEnterUserName = new JLabel("ENTER USER NAME");
		lblEnterUserName.setBounds(10, 65, 135, 27);
		panel.add(lblEnterUserName);
		
		JLabel lblEnterPassword = new JLabel("ENTER PASSWORD");
		lblEnterPassword.setBounds(10, 134, 135, 27);
		panel.add(lblEnterPassword);
		
		user_password = new JTextField("123");
		user_password.setBounds(155, 137, 96, 20);
		panel.add(user_password);
		user_password.setColumns(10);
		
		JLabel lblWelcomeToLogin = new JLabel("WELCOME TO LOGIN WINDOW ");
		lblWelcomeToLogin.setFont(new Font("Tahoma", Font.ITALIC, 15));
		lblWelcomeToLogin.setForeground(Color.BLUE);
		lblWelcomeToLogin.setBounds(97, 11, 245, 14);
		panel.add(lblWelcomeToLogin);
		JButton btnOK = new JButton("OK");

		btnOK.setBounds(67, 215, 80, 23);
		panel.add(btnOK);
		JButton btnCencel = new JButton("CENCEL");
		
		btnCencel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{

				System.exit(0);
			}
		});
			
		btnCencel.setBounds(201, 215, 89, 23);
		panel.add(btnCencel);
		
		//this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		main_frame.add(panel);
		
		btnOK.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				
				password=user_password.getText();
				name=user_name.getText();
				if(name.equalsIgnoreCase("") ||  password.equalsIgnoreCase(""))
				{
				
					showMsg("one of the filed is missing please enter again");
				}
				try {
					GetInformationAndSentToServer(name, password);
				
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			});
		}

	
	public void GetInformationAndSentToServer (String user_id,String pass_user) throws SQLException
	{		
			String msg=new String("SELECT * FROM users where UserID="+user_id);
			client.handleMessageFromClientUI("1;"+msg+";"+user_id+";"+pass_user);
			sermsg = client.sermsg;
			CheckDataFromServer(sermsg.UserName,sermsg.Password,user_id,pass_user,sermsg.User_Type,sermsg.BlockIndex);
	}
	
	//window call number 1 //
	public void CheckDataFromServer (String name_server,String pass_server,String id_client,String pass_client,String User_Type,String BlockIndex) throws SQLException
	{			
		String msg;
		
			if(name_server.equalsIgnoreCase(id_client) && pass_server.equalsIgnoreCase(pass_client))
			{
					
				switch(Integer.parseInt(User_Type))
				{
					case 1:					//type student
						msg=new String("SELECT * FROM students where StudentsID="+id_client);
						client.handleMessageFromClientUI("2;"+msg);	//ask for student detail from database 
						student_obj=client.student_obj;
						
						
					break;
					
					case 2:				//type teachers
						msg=new String("SELECT * FROM teachers where UserName="+id_client);
						client.handleMessageFromClientUI("3;"+msg);		
						teacher_obj=client.teacher_obj;
					break;
					
					case 3:							//type secretary  
						msg=new String("SELECT * FROM SecretaryANDPrincipal where IDsecretaryANDprincipal="+id_client);
						client.handleMessageFromClientUI("4;"+msg+";0");//0 is secretary
						secretary_Principal_obj=client.secretary_Principal_obj;
						windowfuncSecretery();
					break;
					
					case 4:							//type  principal 
						msg=new String("SELECT * FROM SecretaryANDPrincipal where UserName="+id_client);
						client.handleMessageFromClientUI("4;"+msg+";1");//1 is principal
						secretary_Principal_obj=client.secretary_Principal_obj;
						
					break;
						
					case 5:					//type parents
						msg=new String("SELECT * FROM Parents where UserName="+id_client);
						client.handleMessageFromClientUI("5;"+msg);
					break;			
					
				}
			}
			
			
			
			else				//the first condition is wrong the identifier  and password
			{
				
				if (!name_server.equalsIgnoreCase(id_client)) //if the user id is wrong /not equals like the server data error message
				{
					showMsg("user not exist");
					return;
				}
				
				if(!pass_server.equalsIgnoreCase(pass_client))	//if the user password is wrong /not equals like the server data error message 
				{
					showMsg("password not match");
					return;
				}
				
				if((BlockIndex.equalsIgnoreCase("1")))			//if the user is block error message 
				{
					showMsg("your account is block");
					return;
				}
					
			}

			

	}
	
	public void showMsg(String msg)		//Func that sent error message 
	{
		JOptionPane.showMessageDialog(null,msg,"Login Eror",JOptionPane.ERROR_MESSAGE);
	}
	
	public  void windowfuncSecretery ()
	{
		
		main_frame.remove(panel);
		Secretry window1= new Secretry (secretary_Principal_obj);
		window1.client=client;
		main_frame.setVisible(false);
	}
}
